const sectorCompute = ({ areaResult, length, begin, sectors }) =>
  sectors.map((sector) => {
    let evaluationOriginalQuarter = new Array(length).fill(0);
    let evaluationOriginalWhole = new Array(length).fill(0);
    let evaluationCalibratedQuarter = new Array(length).fill(0);
    let evaluationCalibratedWhole = new Array(length).fill(0);
    let areaDetails = new Array(length).fill(null);
    let areaTagDetails = new Array(length).fill(null);
    let models = new Array();
    let seatCount = sector.areas.length;
    areaResult.forEach((result) => {
      if (sector.areas.includes(result.id)) {
        models.push(result.models);
        for (let i = 0; i < length; i++) {
          evaluationOriginalQuarter[i] +=
            result.summary.evaluationOriginalQuarter[i] / seatCount;
          evaluationOriginalWhole[i] +=
            result.summary.evaluationOriginalWhole[i] / seatCount;
          evaluationCalibratedQuarter[i] +=
            result.summary.evaluationCalibratedQuarter[i] / seatCount;
          evaluationCalibratedWhole[i] +=
            result.summary.evaluationCalibratedWhole[i] / seatCount;
          if (areaDetails[i] == null) {
            areaDetails[i] = {};
          }
          areaDetails[i][result.id] = {
            flowOriginalQuarter: result.summary.flowOriginalQuarter[i],
            flowOriginalHalf: result.summary.flowOriginalHalf[i],
            flowOriginalWhole: result.summary.flowOriginalWhole[i],
            flowCalibratedQuarter: result.summary.flowCalibratedQuarter[i],
            flowCalibratedHalf: result.summary.flowCalibratedHalf[i],
            flowCalibratedWhole: result.summary.flowCalibratedWhole[i],
            loadOriginalQuarter: result.summary.loadOriginalQuarter[i],
            loadOriginalHalf: result.summary.loadOriginalHalf[i],
            loadOriginalWhole: result.summary.loadOriginalWhole[i],
            loadCalibratedQuarter: result.summary.loadCalibratedQuarter[i],
            loadCalibratedHalf: result.summary.loadCalibratedHalf[i],
            loadCalibratedWhole: result.summary.loadCalibratedWhole[i],
            evaluationOriginalQuarter:
              result.summary.evaluationOriginalQuarter[i],
            evaluationOriginalWhole: result.summary.evaluationOriginalWhole[i],
            evaluationCalibratedQuarter:
              result.summary.evaluationCalibratedQuarter[i],
            evaluationCalibratedWhole:
              result.summary.evaluationCalibratedWhole[i],
          };
          if (areaTagDetails[i] == null) {
            areaTagDetails[i] = {};
          }
          // console.log("areaTagDetails", result.details);
          areaTagDetails[i][result.id] = result.details.map((tagDetails) => ({
            // area: tagDetails.area,
            // models: tagDetails.models,
            tag: tagDetails.tag,
            // concerned: tagDetails.concerned,
            ratio: tagDetails.ratio,
            flowOriginalQuarter: tagDetails.flowOriginalQuarter[i],
            flowOriginalHalf: tagDetails.flowOriginalHalf[i],
            flowOriginalWhole: tagDetails.flowOriginalWhole[i],
            flowCalibratedQuarter: tagDetails.flowCalibratedQuarter[i],
            flowCalibratedHalf: tagDetails.flowCalibratedHalf[i],
            flowCalibratedWhole: tagDetails.flowCalibratedWhole[i],
            loadOriginalQuarter: tagDetails.loadOriginalQuarter[i],
            loadOriginalHalf: tagDetails.loadOriginalHalf[i],
            loadOriginalWhole: tagDetails.loadOriginalWhole[i],
            loadCalibratedQuarter: tagDetails.loadCalibratedQuarter[i],
            loadCalibratedHalf: tagDetails.loadCalibratedHalf[i],
            loadCalibratedWhole: tagDetails.loadCalibratedWhole[i],
          }));
        }
      }
    });
    let output = {
      title: sector.title,
      modes: sector.modes,
      areas: sector.areas,
      models,
      evaluation: {
        evaluationOriginalQuarter,
        evaluationOriginalWhole,
        evaluationCalibratedQuarter,
        evaluationCalibratedWhole,
      },
      areaDetails,
      areaTagDetails,
    };
    // console.log("sectorCompute", output);
    return output;
  });

export default sectorCompute;
